package com.atguigu.gmallre.mock.stage;

import com.atguigu.gmallre.mock.service.*;
import com.atguigu.mock.util.RandomNumString;
import com.atguigu.gmallre.mock.bean.OrderInfo;
import com.atguigu.gmallre.mock.bean.OrderRefundInfo;
import com.atguigu.gmallre.mock.bean.RefundPayment;
import com.atguigu.gmallre.mock.constant.GmallConstant;
import com.atguigu.gmallre.mock.factory.UserSession;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("end")
public class EndStage implements Stage {

    @Autowired
    LogService logService;

    @Autowired
    OrderInfoService orderInfoService;


    @Autowired
    OrderRefundInfoService orderRefundInfoService;

    @Autowired
    RefundPaymentService refundPaymentService;

    @Autowired
    OrderStatusLogService orderStatusLogService;


    @Override
    public boolean handle() {

        expireOrder();
        finishOrder();
        return true;
    }



    private  void expireOrder(){

        OrderInfo orderInfo  = UserSession.getOrderInfo();
        if(orderInfo!=null) {
             QueryWrapper<OrderInfo> unpaidQuery = new QueryWrapper<OrderInfo>().eq("user_id", UserSession.getUserInfo().getId()).eq("order_status", GmallConstant.ORDER_STATUS_UNPAID);
            List<OrderInfo> uppaidOrderInfoList = orderInfoService.listWithDetail(unpaidQuery, false);
            for (OrderInfo unpaidOrderInfo : uppaidOrderInfoList) {
                unpaidOrderInfo.setOrderStatus(GmallConstant.ORDER_STATUS_CANCEL);
                unpaidOrderInfo.setExpireTime(DateUtils.addMinutes(orderInfo.getCreateTime(), 10));

                orderInfoService.saveOrUpdate(unpaidOrderInfo);
            }
        }

    }

    private  void finishOrder(){
        QueryWrapper<OrderInfo> refundingQuery = new QueryWrapper<OrderInfo>().eq("user_id", UserSession.getUserInfo().getId()).eq("order_status", GmallConstant.ORDER_STATUS_REFUNDING);
        List<OrderInfo> refundingOrderInfoList = orderInfoService.listWithDetail(refundingQuery, false);
        for (OrderInfo refundingOrderInfo : refundingOrderInfoList) {
            //原始单的退单处理
            refundingOrderInfo.setOrderStatus(GmallConstant.ORDER_STATUS_REFUND_DONE);
            refundingOrderInfo.setOperateTime( UserSession.getCurDateTime()  );
            orderInfoService.saveOrUpdate(refundingOrderInfo);
            orderStatusLogService.genOrderStatusLog(refundingOrderInfo);


        }


        QueryWrapper<OrderRefundInfo> orderRefundQuery = new QueryWrapper<OrderRefundInfo>().eq("user_id", UserSession.getUserInfo().getId()).eq("refund_status", GmallConstant.REFUND_STATUS_APPROVING);
        List<OrderRefundInfo>  orderRefundInfoList = orderRefundInfoService.list(orderRefundQuery);
        UserSession.addTimeByDuringTime(3600*1000);
        for (OrderRefundInfo orderRefundInfo : orderRefundInfoList) {

            orderRefundInfo.setRefundStatus(GmallConstant.ORDER_STATUS_REFUND_DONE);
            orderRefundInfo.setOperateTime(DateUtils.addHours(UserSession.getCurDateTime(), 1));
            orderRefundInfoService.saveOrUpdate(orderRefundInfo);
           //退款单处理
            RefundPayment refundPayment = genRefundPayment(orderRefundInfo);
            refundPaymentService.saveOrUpdate(refundPayment);
            UserSession.addTimeByDuringTime(5*1000);
            refundPayment.setCallbackTime( UserSession.getCurDateTime() );
            refundPayment.setCallbackContent("xxxxxxxxxxxxxxx");
            refundPayment.setRefundStatus(GmallConstant.PAYMENT_STATUS_DONE);
            refundPaymentService.saveOrUpdate(refundPayment);
              //退款单支付处理
            orderRefundInfo.setRefundStatus(GmallConstant.REFUND_STATUS_FINISH);

            orderRefundInfo.setOperateTime(UserSession.getCurDateTime());
            orderRefundInfoService.saveOrUpdate(orderRefundInfo);
        }




    }

    public RefundPayment genRefundPayment( OrderRefundInfo orderRefundInfo){
        RefundPayment refundPayment = new RefundPayment();
        refundPayment.setOrderId(orderRefundInfo.getOrderId() );

        refundPayment.setSkuId(  orderRefundInfo.getSkuId() );
        refundPayment.setPaymentType(GmallConstant.PAYMENT_TYPE_ALIPAY);
        refundPayment.setRefundStatus(GmallConstant.PAYMENT_STATUS_TODO);

        refundPayment.setTotalAmount(orderRefundInfo.getRefundAmount() );
        refundPayment.setCreateTime(UserSession.getCurDateTime());
        refundPayment.setTradeNo(RandomNumString.getRandNumString(1,9,15,""));
        refundPayment.setSubject("退款");

        return refundPayment;

    }

}
